using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Utils.Text;
using Utils.Drawing;

namespace Utils.Drawing
{
    public class TextField
    {
        public static readonly Vector2 ORIGIN_CENTER = new Vector2(0.5f, 0.5f);
        public static readonly Vector2 ORIGIN_TOP = new Vector2(0.5f, 0);
        public static readonly Vector2 ORIGIN_BOTTOM = new Vector2(0.5f, 1);
        public static readonly Vector2 ORIGIN_LEFT = new Vector2(0, 0.5f);
        public static readonly Vector2 ORIGIN_RIGHT = new Vector2(1, 0.5f);
        public static readonly Vector2 ORIGIN_TOP_LEFT = new Vector2(0, 0);
        public static readonly Vector2 ORIGIN_TOP_RIGHT = new Vector2(1, 0);
        public static readonly Vector2 ORIGIN_BOTTOM_LEFT = new Vector2(0, 1);
        public static readonly Vector2 ORIGIN_BOTTOM_RIGHT = new Vector2(1, 1);

        LightString text;
        bool textChanges = true;
        SpriteFont font;
        Vector2 pos;
        Vector2 origin = ORIGIN_CENTER;
        Vector2 originPos;
        Vector2 scale = Vector2.One;
        float rotation;
        Color color = Color.White;
        float alpha = 1;
        BlendState blendState = BlendState.AlphaBlend;

        public LightString Text { get { textChanges = true; return text; } set { textChanges = true;  text = value; } }
        public SpriteFont Font { get { return font; } set { font = value; } }
        public Vector2 Pos { get { return pos; } set { pos = value; } }
        public Vector2 Origin { get { return origin; } set { origin = value; } }
        public Vector2 Scale { get { return scale; } set { scale = value; } }
        public float Rotation { get { return rotation; } set { rotation = value; } }
        public Color Color { get { return color; } set { color = value; } }
        public float Alpha { get { return alpha; } set { alpha = value; } }
        public BlendState BlendState { get { return blendState; } set { blendState = value; } }

        public TextField(SpriteFont font, int capacity)
        {
            this.font = font;
            text = new LightString(capacity);
        }

        public TextField(SpriteFont font, LightString text)
        {
            this.font = font;
            this.text = text;
        }

        public void Draw(SpriteRenderer renderer)
        {
            if (textChanges)
            {
                if (origin == ORIGIN_TOP_LEFT)
                {
                    originPos = Vector2.Zero;
                }
                else
                {
                    originPos = font.MeasureString(text.StringBuilder);
                    originPos.X *= origin.X;
                    originPos.Y *= origin.Y;
                }

                textChanges = false;
            }

            Color col;
            if (blendState == BlendState.AlphaBlend || blendState == SpriteRenderer.AdditivePremultiplied)
            {
                col = color * alpha;
            }
            else
            {
                col = color;
                col.A = (byte)(col.A * alpha);
            }

            renderer.DrawText(text, font, pos, originPos, scale, rotation, col);
        }
    }
}
